package com.csxy.nyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.nyf.api.ServiceEduClient;
import com.csxy.nyf.api.ServiceUcenterClient;
import com.csxy.nyf.entity.TOrder;
import com.csxy.nyf.entity.vo.EduCourseVo;
import com.csxy.nyf.entity.vo.UcenterMemberVo;
import com.csxy.nyf.mapper.OrderMapper;
import com.csxy.nyf.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, TOrder> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ServiceEduClient serviceEduClient;

    @Autowired
    private ServiceUcenterClient serviceUcenterClient;

    @Override
    public String createOrders(String courseId, String memberId) {
        //查询用户信息
        UcenterMemberVo member = serviceUcenterClient.getMemberInfoById(memberId);
        //查询课程信息
        EduCourseVo courseInfo = serviceEduClient.getCourseInfoByIdOrder(courseId);
        TOrder tOrder = new TOrder();
        tOrder.setMobile(member.getMobile());
        tOrder.setNickname(member.getNickname());
        tOrder.setMemberId(memberId);
        tOrder.setCourseCover(courseInfo.getCover());
        tOrder.setCourseId(courseId);
        tOrder.setCourseTitle(courseInfo.getTitle());
        tOrder.setTeacherName(courseInfo.getTeacherName());
        tOrder.setTotalFee(courseInfo.getPrice());
        tOrder.setStatus(0);  //支付状态（0-已支付 1-未支付）
        tOrder.setPayType(1); //支付类型（1：微信 2：支付宝）
        tOrder.setOrderNo("ORD-"+ UUID.randomUUID().toString().replace("-", "").substring(0, 8));
        //保存订单
        baseMapper.insert(tOrder);
        //返回订单号
        return tOrder.getOrderNo();


    }

    @Override
    public TOrder getOrderByTradeNo(String tradeNo) {
        // 示例：根据订单号查询
        QueryWrapper<TOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", tradeNo); // 假设字段名为order_no
        return orderMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateOrder(TOrder order) {
        orderMapper.updateById(order);
    }


}
